/*0GI1xcoeligdpMeXoHBphnGa/rC0a0nsL/Q+v3pf6vA0WCzqThDDgpmEEbYpGNOy*/
import { ajax, base, toast ,promptBox} from 'nc-lightapp-front';
import { setAllFieldEditable, setFieldDisabledAP } from '../../../public/publicMethods/pubUtils.js';
import {
	cytjCode,
	hxgzCode,
	hxfsCode,
	searchId,
	tableId,
	appCode,
	pageCode,
	funCode,
	bfCode,
	dfCode,
	publicCode
} from '../constants';

export default function(that, props, key, text, record, index) {
	switch (key) {
		case 'Edit_inner':
			that.pk_bill = record.values.pk_verify_fa.value;
			that.ts = record.values.ts.value;
			//前端需要传递（pk_bill	cytjCode	hxgzCode	hxfsCode	 pageId）
			ajax({
				url: '/nccloud/arap/verifyfa/edit.do',
				data: {
					pk_bill: record.values.pk_verify_fa.value,
					pageId: pageCode,
					cytjCode: cytjCode,
					hxgzCode: hxgzCode,
					hxfsCode: hxfsCode
				},
				success: (res) => {
					if (res.success) {
						if (res.data) {
							writeBack(that, res, props);
						}
					}
				},
				error: function(data) {
					toast({ color: 'danger', content: data.message });
				}
			});

			props.search.openAdvSearch(searchId, true);
			break;
		case 'Delete_Inner':
			that.pk_bill = record.values.pk_verify_fa.value;
			that.ts = record.values.ts.value;
			promptBox({
				color: 'warning',
				title: that.state.json['verificationsheet-000133'] /* 国际化处理： 删除*/,
				content: that.state.json['verificationsheet-000089'] /* 国际化处理： ​确定要删除吗？*/,
				noFooter: false,
				noCancelBtn: false,
				beSureBtnName: that.state.json['verificationsheet-000103'] /* 国际化处理： 确定*/,
				cancelBtnName: that.state.json['verificationsheet-000049'] /* 国际化处理： 取消*/,
				beSureBtnClick: that.delConfirm
			});
			break;
		case 'SetDefault_inner':
			ajax({
				url: '/nccloud/arap/verifyfa/setdefault.do',
				data: {
					pk_bill: record.values.pk_verify_fa.value,
					ts: record.values.ts.value,
					pageId: pageCode,
					funCode: funCode
				},
				success: (res) => {
					if (res) {
						props.editTable.setTableData(tableId, res.data[tableId]);
						toast({
							content: that.state.json['verificationsheet-000094'],
							color: 'success'
						}); /* 国际化处理： 操作成功*/
					}
				},
				error: function(data) {
					toast({ color: 'warning', content: data.message });
				}
			});
			break;
		case 'CancelDefault_inner':
			ajax({
				url: '/nccloud/arap/verifyfa/canceldefault.do',
				data: {
					pk_bill: record.values.pk_verify_fa.value,
					ts: record.values.ts.value,
					pageId: pageCode,
					funCode: funCode
				},
				success: (res) => {
					if (res) {
						props.editTable.setTableData(tableId, res.data[tableId]);
						toast({
							content: that.state.json['verificationsheet-000094'],
							color: 'success'
						}); /* 国际化处理： 操作成功*/
					}
				},
				error: function(data) {
					toast({ color: 'warning', content: data.message });
				}
			});
			break;
		case 'Copy_inner':
			let pk_fa = record.values.pk_verify_fa.value;
			that.props.modal.show('Copyform');
			that.pk_org = null;
			that.props.form.setFormStatus("Copyform",'edit')
			that.pkfa = pk_fa;
			break;
		default:
			break;
	}
}

function writeBack(that, res, props) {
	if (res.data[0]) {
		props.form.EmptyAllFormValue(cytjCode);
		props.form.setAllFormValue({ [cytjCode]: res.data[0]['cytjArea'] });
		//设置常用条件所有字段可编辑
		let arr = [cytjCode, bfCode, dfCode, publicCode]
		setAllFieldEditable(props, cytjCode, arr, true);
		//设置方案名称不可编辑
		props.form.setFormItemsDisabled(cytjCode, { name: true });
		that.editflag  = true;
		//根据数据控制字段可编辑性
		setFieldDisabledAP(props,cytjCode)
		//根据本方核销对象设置本方对象名称参照
		let meta = props.meta.getMeta();
		let jf_hxdxCBox = props.form.getFormItemsValue(cytjCode, 'jf_hxdxCBox').value;
		let df_hxdxCBox = props.form.getFormItemsValue(cytjCode, 'df_hxdxCBox').value;
		let jfTradeRefBox = props.form.getFormItemsValue(cytjCode, 'jfTradeRef').value;
		let dfTradeRefBox = props.form.getFormItemsValue(cytjCode, 'dfTradeRef').value;
		setRefCodeByHxdx(that, props, meta, jf_hxdxCBox, 'jf_dxmcRef', bfCode);
		setRefCodeByHxdx(that, props, meta, df_hxdxCBox, 'df_dxmcRef', dfCode);
		setRefCodeByHxdx(that, props, meta, jfTradeRefBox, 'jfTradeRef', dfCode);
		setRefCodeByHxdx(that, props, meta, dfTradeRefBox, 'dfTradeRef', dfCode);
	}
	if (res.data[1]) {
		props.form.EmptyAllFormValue(hxgzCode);
		props.form.setAllFormValue({ [hxgzCode]: res.data[1][hxgzCode] });
		//设置金额方向不可编辑
		let hldcChkBox = props.form.getFormItemsValue(hxgzCode, 'hldcChkBox');
		if (hldcChkBox.value == true) {
			props.form.setFormItemsDisabled(hxgzCode, { jefxCmbBox: true });
			props.form.setFormItemsRequired(hxgzCode, {'jefxCmbBox':false})
		} else {
			props.form.setFormItemsDisabled(hxgzCode, { jefxCmbBox: false });
			props.form.setFormItemsRequired(hxgzCode, {'jefxCmbBox':true})
		}
	}
	if (res.data[2]) {
		props.form.EmptyAllFormValue(hxfsCode);
		props.form.setAllFormValue({ [hxfsCode]: res.data[2][hxfsCode] });
	}
}

function setRefCodeByHxdx(that, props, meta, hxdxValue, dxmcField, areaCode) {
	if (hxdxValue == 'kh') {
		meta[areaCode].items.map((item) => {
			if (item.attrcode == dxmcField) {
				item.itemtype = 'refer';
				item.refName = that.state.json['verificationsheet-000038']; /* 国际化处理： 客户档案*/
				item.refcode = 'uapbd/refer/customer/CustomerDefaultTreeGridRef/index';
				item.queryCondition = () => {
					return {
						DataPowerOperationCode: 'fi', //使用权组
						isDataPowerEnable: 'Y',
						pk_org: props.form.getFormItemsValue(cytjCode, 'pk_org').value
							? props.form.getFormItemsValue(cytjCode, 'pk_org').value
							: null
					};
				};
			}
			return item;
		});
	} else if (hxdxValue == 'gys') {
		meta[areaCode].items.map((item) => {
			if (item.attrcode == dxmcField) {
				item.itemtype = 'refer';
				item.refName = that.state.json['verificationsheet-000039']; /* 国际化处理： 供应商档案*/
				item.refcode = 'uapbd/refer/supplier/SupplierRefTreeGridRef/index';
				item.queryCondition = () => {
					return {
						DataPowerOperationCode: 'fi', //使用权组
						isDataPowerEnable: 'Y',
						pk_org: props.form.getFormItemsValue(cytjCode, 'pk_org').value
							? props.form.getFormItemsValue(cytjCode, 'pk_org').value
							: null
					};
				};
			}
			return item;
		});
	} else if (hxdxValue == 'bm') {
		meta[areaCode].items.map((item) => {
			if (item.attrcode == dxmcField) {
				item.itemtype = 'refer';
				item.refName = that.state.json['verificationsheet-000002']; /* 国际化处理： 部门*/
				item.refcode = 'uapbd/refer/org/DeptTreeRef/index';
				(item.isShowUnit = true),
					(item.unitCondition = () => {
						return {
							pkOrgs: props.form.getFormItemsValue(cytjCode, 'pk_org').value
								? props.form.getFormItemsValue(cytjCode, 'pk_org').value
								: null,
							TreeRefActionExt: 'nccloud.web.arap.ref.before.OrgSqlBuilder'
						};
					});
				item.queryCondition = () => {
					return {
						DataPowerOperationCode: 'fi', //使用权组
						isDataPowerEnable: 'Y',
						busifuncode: 'all',
						pk_org: props.form.getFormItemsValue(cytjCode, 'pk_org').value
							? props.form.getFormItemsValue(cytjCode, 'pk_org').value
							: null
					};
				};
			}
			return item;
		});
	} else if (hxdxValue == 'ywy') {
		meta[areaCode].items.map((item) => {
			if (item.attrcode == dxmcField) {
				item.itemtype = 'refer';
				item.refName = that.state.json['verificationsheet-000042']; /* 国际化处理： 人员*/
				item.refcode = 'uapbd/refer/psninfo/PsndocTreeGridRef/index';
				(item.isShowUnit = true),
					(item.unitCondition = () => {
						return {
							pkOrgs: props.form.getFormItemsValue(cytjCode, 'pk_org').value
								? props.form.getFormItemsValue(cytjCode, 'pk_org').value
								: null,
							TreeRefActionExt: 'nccloud.web.arap.ref.before.OrgSqlBuilder'
						};
					});
				item.queryCondition = () => {
					return {
						DataPowerOperationCode: 'fi', //使用权组
						isDataPowerEnable: 'Y',
						busifuncode: 'all',
						pk_org: props.form.getFormItemsValue(cytjCode, 'pk_org').value
							? props.form.getFormItemsValue(cytjCode, 'pk_org').value
							: null
					};
				};
			}
			return item;
		});
	}
	//重新渲染本方对象名称、对方对象名称
	props.renderItem('form', areaCode, dxmcField, null);
}

/*0GI1xcoeligdpMeXoHBphnGa/rC0a0nsL/Q+v3pf6vA0WCzqThDDgpmEEbYpGNOy*/